package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Purchase;
import com.zyd.shiro.business.enums.PurWay;
import com.zyd.shiro.business.service.PakPurchaseService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakPurchase;
import com.zyd.shiro.persistence.mapper.PakLocationMapper;
import com.zyd.shiro.persistence.mapper.PakPurchaseMapper;
import com.zyd.shiro.persistence.mapper.SysDeptMapper;
import com.zyd.shiro.persistence.mapper.SysOrgMapper;
import com.zyd.shiro.utils.ExcelTool;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class PakPurchaseServiceImpl implements PakPurchaseService {

    @Autowired
    private PakPurchaseMapper pakPurchaseMapper;

    @Resource
    private SysOrgMapper sysOrgMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private PakLocationMapper pakLocationMapper;


    @Override
    public Purchase insert(Purchase purchase) {
        Assert.notNull(purchase, "对象不可为空！");
        purchase.getPakPurchase().setUnicode("CG"+System.currentTimeMillis());
        purchase.getPakPurchase().setAppstatus(0);
        purchase.getPakPurchase().setWarestatus(0);
        purchase.getPakPurchase().setUpdateTime(new Date());
        purchase.getPakPurchase().setCreateTime(new Date());
        pakPurchaseMapper.insertSelective(purchase.getPakPurchase());
        return purchase;
    }

    @Override
    public void insertList(List<Purchase> purchases) {
            for(Purchase purchase:purchases){
               purchase.getPakPurchase().setUpdateTime(new Date());
               purchase.getPakPurchase().setCreateTime(new Date());
               pakPurchaseMapper.insertSelective(purchase.getPakPurchase());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakPurchaseMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Purchase purchase) {
        Assert.notNull(purchase, "对象不可为空！");
        purchase.setUpdateTime(new Date());
        return pakPurchaseMapper.updateByPrimaryKey(purchase.getPakPurchase()) > 0;
    }

    @Override
    public boolean updateSelective(Purchase purchase) {
        Assert.notNull(purchase, "对象不可为空！");
        purchase.setUpdateTime(new Date());
        return pakPurchaseMapper.updateByPrimaryKeySelective(purchase.getPakPurchase()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Purchase getByPrimaryKey(Long primaryKey) {
        PakPurchase pakPurchase = pakPurchaseMapper.selectByPrimaryKey(primaryKey);
        return null == pakPurchase ? null : new Purchase(pakPurchase);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Purchase getOneByEntity(Purchase test) {
        Assert.notNull(test, "对象不可为空！");
        PakPurchase pakPurchase = pakPurchaseMapper.selectOne(test.getPakPurchase());
        return null == pakPurchase ? null : new Purchase(pakPurchase);
    }

    @Override
    public List<Purchase> listAll() {
        List<PakPurchase> pakPurchases = pakPurchaseMapper.selectAll();
        if (CollectionUtils.isEmpty(pakPurchases)) {
            return null;
        }
        List<Purchase> purchases = new ArrayList<>();
        for (PakPurchase pakPurchase : pakPurchases) {
            purchases.add(new Purchase(pakPurchase));
        }
        return purchases;
    }

    @Override
    public List<Purchase> listByEntity(Purchase entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Purchase> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakPurchase> pakPurchases = pakPurchaseMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakPurchases)) {
            return null;
        }
        List<Purchase> purchases = new ArrayList<>();
        for (PakPurchase pakPurchase : pakPurchases) {
            purchases.add(new Purchase(pakPurchase));
        }
        PageInfo bean = new PageInfo<PakPurchase>(pakPurchases);
        bean.setList(purchases);
        return bean;
    }
    @Override
    public void increase(PakPurchase pakPurchase) {
        pakPurchaseMapper.insert(pakPurchase);
    }

    @Override
    public Purchase mapConvObject(Map<String, String> map) {

        PakPurchase pakPurchase = new PakPurchase();
        try {
            pakPurchase.setApplyTime(ExcelTool.simFormat.parse(map.get("applyTime")));
        } catch (ParseException e) {
            pakPurchase.setApplyTime(null);
        }
        pakPurchase.setWay(PurWay.getCodeByMsg(map.get("way")));
        pakPurchase.setRemark(map.get("remark"));
        pakPurchase.setAmt(Double.valueOf(map.get("amt")));
        pakPurchase.setLocation(pakLocationMapper.findIdByName(map.get("locationName").trim()));
        pakPurchase.setDept(sysDeptMapper.findIdByName(map.get("deptName").trim()));
        pakPurchase.setOrg(sysOrgMapper.findIdByName(map.get("orgName").trim()));
        pakPurchase.setLocationName(map.get("locationName").trim());
        pakPurchase.setDeptName(map.get("deptName").trim());
        pakPurchase.setOrgName(map.get("orgName").trim());
        return insert(new Purchase(pakPurchase));
    }


}
